package com.shylock.as.manager.impl;

import com.shylock.as.entity.OssLog;
import com.shylock.as.manager.intf.AliOssService;
import com.shylock.as.service.intf.OssLogService;
import com.shylock.as.util.AliOssUtils;
import com.shylock.common.config.SystemConfig;
import com.shylock.common.util.DateUtil;
import com.shylock.common.util.FileUtils;
import com.shylock.common.util.RUtils;
import com.shylock.common.util.StringUtils;
import com.shylock.common.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AliOssServiceImpl implements AliOssService {
    @Autowired
    private OssLogService service;
    @Override
    public R upload(String fn, byte[] data) {
        fn = FileUtils.rename(fn);
        if(data.length>0){
            Date cdate = DateUtil.getDateYear(SystemConfig.OSS_URL_TIME);
            String url = AliOssUtils.upload(fn, data, cdate);
            if(StringUtils.isNotEmpty(url)){
             
                //新增数据库记录
                service.save(new OssLog(SystemConfig.OSS_TYPE_HEAD,AliOssUtils.bucketName,fn,url,cdate,new Date()));
                RUtils.ok(url);
            }
        }
        return RUtils.fail("上传失败");
    }
}
